import str from '@/util/str.js'

import {
	postMultiGet
} from '@/api/currency.js'

// 记录来源参数
var ref_query = '';

// 朋友圈单独调用init();
function friends() {
	let options = wx.getEnterOptionsSync();
	if (options.scene == 1154 || options.scene == 1155) {
		init();
	}
}

// 获取启动参数并写入缓存
function init() {
	let options = wx.getEnterOptionsSync();
	switch (options.scene) {
		case 1011:
			// 扫描二维码
		case 1012:
			// 长按识别
		case 1013:
			// 选择相册图片识别
			enterByScanQrcode(options);
			break;
			// 个人分享
		case 1007:
			// 群聊
		case 1008:
			enterByShareMessage(options);
			break;
			// 朋友圈
		case 1154:
		case 1155:
			enterByShareFriends(options)
			break;
		default:
			// todo  不知道的一律跳首页
	}
}

// 通过朋友圈点击进入
function enterByShareFriends(options) {
	ref_query = objAdnString(options);
	console.log(ref_query);
	ref_queryData(options);
}

// 通过识别二维码方式进入
function enterByScanQrcode(options) {
	// q是识别普通二维码的参数，小程序二维码不是
	let q = str.get(options.query, 'q', '');

	if (!q) {
		// 默认跳转到首页
		uni.switchTab({
			url: '/pages/index/index'
		})
		return;
	}
	// 解码
	let url = decodeURIComponent(q)
	let urlObj = str.parseURL(url);
	// 跳转路径,默认是首页
	let go = '/pages/index/index';
	let query = {};
	// ref_query = urlObj.query
	ref_query = url
	if (urlObj.query) {
		query = str.parseUrlQuery(urlObj.query);
	}

	// 有个跳转工人详情的二维码
	if (urlObj.path == '/pages/worker/detail' && query?.worker_id) {
		uni.reLaunch({
			url: '/pages/worker/detail?worker_id=' + query?.worker_id,
		});
		return;
	}

	// 处理跳转逻辑
	if (query?.go) {
		go = decodeURIComponent(query.go);
		query = str.parseURL(go)
		// 判断query.path 是不是 tabBar
		let tabBars = ['/pages/index/index', '/pages/order/order/order', '/pages/my_actions/my/my','/pages/classify/classify'];
		if (tabBars.indexOf(query.path) > -1) {
			uni.switchTab({
				url: go,
			})
		} else {
			uni.reLaunch({
				url: go,
			})
		}
	} else {
		uni.switchTab({
			url: go,
		})
	}
	ref_queryData(options);
}

// 通过分享进入
function enterByShareMessage(options) {
	// todo 查看参数自行处理
	let go = decodeURIComponent(options?.query?.go);
	ref_query = objAdnString(options);
	ref_queryData(options);
	console.log(options)
	if (go) {
		let tabBars = ['/pages/index/index', '/pages/order/order/order', '/pages/my_actions/my/my','/pages/classify/classify'];
		if (tabBars.indexOf(go) > -1) {
			uni.switchTab({
				url: go,
			})
		} else {
			uni.reLaunch({
				url: go,
			})
		}
	}
}

// 上报数据
function ref_queryData(options) {
	uni.setStorageSync('ref_query', objAdnString(options));
}

// obj 链接组装
function objAdnString(options) {
	// 域名
	let http = 'https://wxmini.com/';
	let r = [];
	for (let k in options?.query) {
		// 这里没考虑二维数组等
		r.push('' + k + '=' + options?.query[k]);
	}

	let refStr = "";
	if (r.length > 0) {
		refStr = r.join('&');
	}

	let path = `${http}${options.path}?${refStr}`
	uni.setStorageSync('path', path);
	return path;
}


/**
 * 分享的时候调用
 * @see https://developers.weixin.qq.com/minigame/dev/api/share/wx.onShareAppMessage.html
 * @param {object} obj 参数同 wx.onShareAppMessage(),但是会对path做拦截到 pages/share/share 做统一处理
 */

// 存储路径

let share_cover = '';
async function onShare(obj) {
	if (!obj.title) {
		obj.title = "心宇利家政"
	}
	let uid = "";
	if (uni.getStorageSync('userId')) {
		uid = uni.getStorageSync('userId');
	}
	console.log(obj)

	obj.path = `/pages/share/share?ref=wxmini_user_share&uid=${uid}&go=${encodeURIComponent(obj.path)}&activity_id=${obj.activityId}`;

	if (!obj.imageUrl) {
		if (share_cover == '') {
			let res = await postMultiGet({ codes: 'share_card_cover' })
			if (res.code == 200) {
				let { share_card_cover } = res.data.lists;
				obj.imageUrl = share_card_cover;
				share_cover = share_card_cover;
			}
		} else {
			obj.imageUrl = share_cover;
		}
	}
	return obj
}

// 注册的时候要的参数
function getRefQuery() {
	return ref_query;
}

export default {
	init,
	onShare,
	getRefQuery,
	friends
}